package com.xms.organization.api.controller;


import com.xms.core.config.SystemEntityNames;
import com.xms.core.data.Entity;
import com.xms.core.exception.XmsException;
import com.xms.organization.api.model.AddTeamMembersModel;
import com.xms.organization.api.model.RemoveTeamMembersModel;
import com.xms.sdk.core.query.ConditionOperator;
import com.xms.sdk.core.query.QueryExpression;
import com.xms.sdk.service.IDataCreater;
import com.xms.sdk.service.IDataDeleter;
import com.xms.sdk.service.IDataFinder;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-10-10
 */
@RestController
@RequestMapping("/{org}/api/team")
public class TeamController extends ApiBaseController {
    private IDataFinder _dataFinder;
    private IDataCreater _dataCreater;
    private IDataDeleter _dataDeleter;
    @Autowired
    public TeamController(IDataFinder dataFinder
            , IDataCreater dataCreater
            , IDataDeleter dataDeleter)
    {
        _dataFinder = dataFinder;
        _dataCreater = dataCreater;
        _dataDeleter = dataDeleter;
    }

    @ApiOperation("添加团队成员")
    @PostMapping("addmembers")
    public JsonResultObject addMembers(@RequestBody AddTeamMembersModel model) throws XmsException, SQLException {
        if (CollectionUtil.isEmpty(model.userId))
        {
            return JNotSpecifiedRecord();
        }
        QueryExpression query = new QueryExpression(SystemEntityNames.TeamMemberShip);
        query.ColumnSet.addColumns("systemuserid");
        query.Criteria.addCondition("teamid", ConditionOperator.Equal, model.teamId);
        List<Entity> members = _dataFinder.retrieveAll(query);
        List<Entity> addEntities = new ArrayList<>();
        for (UUID item : model.userId)
        {
            if (!members.stream().anyMatch(n -> n.getUUIDValue("systemuserid").equals(item)))
            {
                Entity entity = new Entity(SystemEntityNames.TeamMemberShip);
                entity.setValue("teamid", model.teamId);
                entity.setValue("systemuserid", item);
                addEntities.add(entity);
            }
        }
        if (CollectionUtil.notEmpty(addEntities))
        {
            _dataCreater.createMany(addEntities);
        }
        return JOk();
    }

    @ApiOperation("移除团队成员")
    @PostMapping("removemembers")
    public JsonResultObject removeMembers(@RequestBody RemoveTeamMembersModel model) throws XmsException, SQLException {
        if (CollectionUtil.isEmpty(model.userId))
        {
            return JNotSpecifiedRecord();
        }
        QueryExpression query = new QueryExpression(SystemEntityNames.TeamMemberShip);
        query.ColumnSet.addColumns("systemuserid");
        query.Criteria.addCondition("teamid", ConditionOperator.Equal, model.teamId);
        query.Criteria.addCondition("systemuserid", ConditionOperator.In, model.userId);
        List<Entity> members = _dataFinder.retrieveAll(query);
        for (Entity item : members)
        {
            _dataDeleter.delete(item);
        }
        return JDeleteSuccess();
    }
}
